package com.zy.scwuser.service.impl;

import java.util.List;
import java.util.UUID;

import com.zy.scwcommon.entity.TMember;
import com.zy.scwcommon.entity.TMemberAddress;
import com.zy.scwcommon.entity.TMemberAddressExample;
import com.zy.scwcommon.entity.TMemberExample;
import com.zy.scwuser.enums.UserExceptionEnum;
import com.zy.scwuser.exp.UserException;
import com.zy.scwuser.mapper.TMemberAddressMapper;
import com.zy.scwuser.mapper.TMemberMapper;
import com.zy.scwuser.service.TMemberService;
import com.zy.scwuser.vo.req.UserRegistVo;
import com.zy.scwuser.vo.resp.UserRespVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
@Transactional(readOnly=true)
public class TMemberServiceImpl implements TMemberService {
	@Autowired
	TMemberMapper memberMapper;
	
	@Autowired
	TMemberAddressMapper tMemberAddressMapper;
	@Autowired
	StringRedisTemplate stringRedisTemplate;

	//@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.REPEATABLE_READ,rollbackFor=RuntimeException.class)
	@Transactional
	@Override
	public int saveTMember(UserRegistVo vo) {

		try {
			//将vo属性对拷到do对象中
			TMember member = new TMember();
			BeanUtils.copyProperties(vo, member);
			member.setUsername(vo.getLoginacct());//loginacct和username值一样
			String userpswd = vo.getUserpswd();
			BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();//把密码进行加密Spring Security里的方法---也可解密
			member.setUserpswd(encoder.encode(userpswd));
			int i = memberMapper.insertSelective(member);
			log.debug("注册会员成功-{}",member);
			return i;
		} catch (Exception e) {
			e.printStackTrace();
			log.error("注册会员失败-{}",e.getMessage());
			//throw new RuntimeException(UserExceptionEnum.USER_SAVE_ERROR.getMessage());
			throw new UserException(UserExceptionEnum.USER_SAVE_ERROR);
		}
	}

	@Override
	public UserRespVo getUserByLogin(String loginacct, String password) {

		UserRespVo vo = new UserRespVo();

		TMemberExample example = new TMemberExample();
		example.createCriteria().andLoginacctEqualTo(loginacct);

		List<TMember> list = memberMapper.selectByExample(example);
		if(list==null || list.size()==0) {
			//throw new RuntimeException(UserExceptionEnum.USER_UNEXISTS.getMessage());
			throw new UserException(UserExceptionEnum.USER_UNEXISTS);
		}

		TMember member = list.get(0);
		BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
		if(!encoder.matches(password, member.getUserpswd())) { //密码比较
		//	throw new RuntimeException(UserExceptionEnum.USER_PASSWORD_ERROR.getMessage());
			throw new UserException(UserExceptionEnum.USER_PASSWORD_ERROR);
		}

		BeanUtils.copyProperties(member, vo);

		String accessToken = UUID.randomUUID().toString().replaceAll("-", "");
		vo.setAccessToken(accessToken);

		stringRedisTemplate.opsForValue().set(accessToken, member.getId().toString());

		return vo;
	}

	@Override
	public TMember selectById(Integer id) {
		TMember tMember = memberMapper.selectByPrimaryKey(id);
		return tMember;
	}

	@Override
	public List<TMemberAddress> listAddredd (Integer id) {
		TMemberAddressExample tMemberAddressExample = new TMemberAddressExample ();
		tMemberAddressExample.createCriteria ().andMemberidEqualTo (id);
		List<TMemberAddress> list = tMemberAddressMapper.selectByExample (tMemberAddressExample);
		return list;
	}

}
